package cn.hxlc.tie.demo.modular.zr.equipmentzr.service.impl;/*
 @author rui
 @date 2021/6/28 --19:04
*/

import cn.hutool.core.bean.BeanUtil;
import cn.hxlc.tie.demo.core.enums.StatusEnum;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.entity.Equipment;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.mapper.EquipmentMapper;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.model.param.EquipmentParam;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.model.result.EquipmentResult;
import cn.hxlc.tie.demo.modular.zr.equipmentzr.service.EquipmentService;
import cn.hxlc.tie.libs.mp.page.PageFactory;
import cn.hxlc.tie.model.page.PageResult;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, Equipment> implements EquipmentService {

    @Autowired
    private EquipmentMapper equipmentMapper;
    private Equipment getEntity(EquipmentParam equipmentParam) {
        Equipment entity=new Equipment();
        BeanUtil.copyProperties(equipmentParam, entity);
        return entity;
    }
    private Serializable getKey(EquipmentParam param){
        return param.getFid();
    }
    @Override
    public PageResult<EquipmentResult> queryListPage(EquipmentParam equipmentParam) {
        Page<EquipmentResult> equipmentPage= PageFactory.defaultPage();
        IPage<EquipmentResult> resultPage= equipmentMapper.queryListPage(equipmentPage,equipmentParam);
        return new PageResult<>(resultPage);
    }

    @Override
    public void addOne(EquipmentParam equipmentParam) {
      Equipment entity=getEntity(equipmentParam);
      entity.setFcreatetime(new Date());
      entity.setFstatus(StatusEnum.AUX);
      //entity.setFlevel(LevelEnum.ADVANCE);
        this.baseMapper.insert(entity);
    }

    @Override
    public EquipmentResult detail(Long fid) {
        return equipmentMapper.queryDetail(fid);
    }

    @Override
    public void delete(EquipmentParam Param) {

        this.removeById(getKey(Param));
    }




    @Override
    public Boolean batchEdit(List<EquipmentParam> equipmentParam) {

            return  this.baseMapper.batchEdit(equipmentParam, StatusEnum.DELETED.getCode())>0;
    }

    @Override
    public void batchChangeState(Object[] fid, StatusEnum toEnum) {
        Equipment info ;
       // Long userid = getUserid();
        for(int i=0;i<fid.length;i++){
            info = this.getById(new Long(fid[i].toString()));
            if(info.getFstatus()!=null && toEnum!=null && !info.getFstatus().equals(toEnum)){
                info.setFstatus(toEnum);
                if(toEnum.equals(StatusEnum.BASE)){
                    info.setFcreatetime(new Date());

                }else{
                    info.setFcreatetime(new Date());

                }
                info.setFcreatetime(new Date());
                this.updateById(info);
            }
        }
    }

    @Override
    public void updateOne(EquipmentParam equipmentParam) {
        Equipment entity=getEntity(equipmentParam);
        //UpdateWrapper wrapper=new UpdateWrapper();
        this.baseMapper.updateById(entity);
    }

    @Override
    public EquipmentResult detailList(Long fid) {
        return this.baseMapper.queryDetail(fid);
    }




}
